﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：CategoryAdd.ascx.cs
//
//文件功能描述： 商品类别添加
// 
//创建标识：2007.11.22 王林进
//创建描述：
//
//修改标识：2007.11.29 王林进
//修改描述：根据页面重新设计，代码重写。
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;
/// <summary>
/// 商品类别添加
/// </summary>
public partial class UserControls_CategoryAdd : System.Web.UI.UserControl
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
           //实例化一个服务
            ProductService updateStatus = new ProductService();
            
            //判断大类别的ID是否为空,不为空时进入if执行
            if (Page.PreviousPage != null)
            {
                UserControl uc = (UserControl)Page.PreviousPage.FindControl("CategoryManage1");        

                //if (this.Request.QueryString["UpperCategoryID"] != null)
                if (((Label)uc.FindControl("lblUpperCategoryID")).Text.Trim () != string.Empty) 
                {   //获得大类别的名称
                    //this.txtCategoryName.Text = this.Request.QueryString["UpperCategoryName"];
                    this.txtCategoryName.Text = ((Label)uc.FindControl("lblUpperCategoryName")).Text.Trim();
                   
                    //获得大类别的ID
                    //string strUpperCategoryID = this.Request.QueryString["UpperCategoryID"];
                    string strUpperCategoryID = ((Label)uc.FindControl("lblUpperCategoryID")).Text.Trim();
                    this.hdfUpperCategoryID.Value = strUpperCategoryID;

                    this.lnkOrderManage.Text = "商品类别状态修改";
                    this.ibtnAdd.Visible  = false;
                    this.ibtnCancel.Visible  = false;
                    this.lblAddCategory.Enabled = false;
                    this.ibtUpdate.Enabled = true;
                    this.dropCategory.Visible = false ;

                    try
                    {
                        UpperCategory upperCategory = new UpperCategory();

                        upperCategory = updateStatus.GetUpperCategoryModel(strUpperCategoryID);
                        //获得表示大类别的状态的布尔值
                        bool upperCategorStatus = upperCategory.Status;
                        //如果为false就把下拉框该位禁用
                        if (upperCategorStatus == false)
                        {
                            if (IsPostBack == false)
                                dropCategoryStatus.SelectedIndex = 1;
                        }
                    }
                    catch
                    { }
                }
                //判断小类别的ID是否为空,不为空时进入if执行
                //else if (this.Request.QueryString["LowerCategoryID"] != null)
                else if(((Label )uc.FindControl ("lblLowerCategoryID")).Text.Trim() !=string .Empty)
                {   //获得小类别的名称
                    //this.txtCategoryName.Text = this.Request.QueryString["LowerCategoryName"];
                    this.txtCategoryName.Text = ((Label)uc.FindControl("lblLowerCategoryName")).Text.Trim();
                    
                    //获得小类别的ID
                    //string strLowerCategoryID = this.Request.QueryString["LowerCategoryID"];

                    string strLowerCategoryID = ((Label)uc.FindControl("lblLowerCategoryID")).Text.Trim();
                    this.hdfLowerCategoryID.Value = strLowerCategoryID;

                    string strFatherCategoryID = ((Label)uc.FindControl("lblFatherCategoryID")).Text.Trim();

                    this.lnkOrderManage.Text = "商品类别状态修改";
                    this.ibtnAdd.Visible = false;
                    this.ibtnCancel.Visible = false;

                    this.ibtUpdate.Enabled = true;

                    UpperCategory upperCategoryModel=updateStatus.GetUpperCategoryModel(strFatherCategoryID);

                    string strFatherCategoryName = upperCategoryModel.Name;

                    this.dropCategory.Items.Add(strFatherCategoryName);

                    try
                    {
                        LowerCategory lowerCategory = new LowerCategory();

                        lowerCategory = updateStatus.GetLowerCategoryModel(strLowerCategoryID);
                        //获得表示小类别的状态的布尔值
                        bool lowerCategorStatus = lowerCategory.Status;
                        //如果为false就把下拉框该为禁用
                        if (lowerCategorStatus == false)
                        {
                            if (IsPostBack == false)
                                dropCategoryStatus.SelectedIndex = 1;
                        }
                    }
                    catch
                    { }
                }
            }
            //获取商品类别，并填充相应下拉列表
            else
            {
                if (IsPostBack == false)
                {
                    this.lnkOrderManage.Text = "商品类别添加";
                    this.ibtnAdd.Visible = true;
                    this.ibtnCancel.Visible = true;
                    this.lblAddCategory.Enabled = true;
                    this.ibtUpdate.Visible = false;
                    this.dropCategoryStatus.Enabled = false;
                    updateStatus.GetUpperCategoryListCompleted += new GetUpperCategoryListCompletedEventHandler(updateStatus_GetUpperCategoryListCompleted);
                    //异步调用获得大类别函数
                    updateStatus.GetUpperCategoryListAsync();
                }
            }
       
    }


    /// <summary>
    /// 处理返回数据。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void updateStatus_GetUpperCategoryListCompleted(object sender, GetUpperCategoryListCompletedEventArgs e)
    {
        //把数据源绑定到dropCategory。 
        dropCategory.DataSource = e.Result;
        //绑定字段
        dropCategory.DataTextField = e.Result.Tables[0].Columns[1].ToString();

        //绑定相应要使用的数据的字段
        dropCategory.DataValueField = e.Result.Tables[0].Columns[0].ToString();
        dropCategory.DataBind();

        dropCategory.Items.Insert(0, "该类别作为大类别");
        
    }
   
    
    /// <summary>
    /// 添加商品类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ProductService addCategory = new ProductService();

        this.ibtnAdd.Enabled = true;
        this.ibtnCancel.Enabled = true;
        
        //获取类别名
        string categoryName = txtCategoryName.Text.Trim();
        if (categoryName == string.Empty)
        {
            this.lblAddCategory .Text = "类别名不能为空";
        }
        else
        {
            //获取类别ID
            string UPCategoryID = dropCategory.SelectedValue;
            if (UPCategoryID == "该类别作为大类别")
            {
                //如果用户没有选择类别，类别ID为空
                UPCategoryID = string.Empty;
            }
            bool result = addCategory.AddCategory(categoryName, UPCategoryID);

            if (result == true)
            {
                this.lblAddCategory.Text = "添加成功";
                this.txtCategoryName.Text = string.Empty;
            }
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        this.ibtnAdd.Enabled = true;

        this.ibtnCancel.Enabled = true;

        txtCategoryName.Text = string .Empty ;//清除类别名称 

        this.lblAddCategory.Text =string .Empty ;//清除提示信息

    }

    /// <summary>
    /// 修改商品类别的状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtUpdate_Click(object sender, ImageClickEventArgs e)
    {
        if (this.txtCategoryName.Text.Trim() == string.Empty)
        {
            this.lblMessage.Text = "你已经修改了";
        }
        else
        {
            ProductService updateStatus = new ProductService();
            //初始化blValue变量为false
            bool blValue = false;
            // 获取要把商品修改为的状态的布尔值
            string strCategoryStatus = dropCategoryStatus.SelectedValue;

            bool blCategoryStatus = Boolean.Parse(strCategoryStatus);

            //if (Page.PreviousPage != null)
            //{
            //UserControl uc = (UserControl)Page.PreviousPage.FindControl(@"~/StoreKeeper/CategoryManage1");

            //if (this.Request.QueryString["UpperCategoryID"] != null)
            //if (((Label)uc.FindControl("lblUpperCategoryID")).Text.Trim() != string.Empty)
            if (this.hdfUpperCategoryID.Value.Trim() != string.Empty)
            {   //获得大类别的ID
                //string strUpperCategoryID = this.Request.QueryString["UpperCategoryID"].ToString();
                //string strUpperCategoryID = ((Label)uc.FindControl("lblUpperCategoryID")).Text.Trim();
                string strUpperCategoryID = this.hdfUpperCategoryID.Value.Trim();

                //返回结果表示修改大类别是否成功
                blValue = updateStatus.UpdateUpperCagegoryStatus(strUpperCategoryID, blCategoryStatus);

                if (blValue == true)
                {
                    lblMessage.Text = "修改成功";
                    this.txtCategoryName.Text = string.Empty;
                    //下拉框标示为启用
                    dropCategoryStatus.SelectedIndex = 0;
                }
                else
                {
                    lblMessage.Text = "修改失败";
                    this.txtCategoryName.Text = string.Empty;
                }
            }
            //else if (this.Request.QueryString["LowerCategoryID"] != null)
            //else if (((Label)uc.FindControl("lblLowerCategoryID")).Text.Trim() != string.Empty)
            else if (this.hdfLowerCategoryID.Value.Trim() != string.Empty)
            {
                //获得小类别的ID
                //string strLowerCategoryID = this.Request.QueryString["LowerCategoryID"].ToString();
                //string strLowerCategoryID = ((Label)uc.FindControl("lblLowerCategoryID")).Text.Trim();
                string strLowerCategoryID = this.hdfLowerCategoryID.Value.Trim();

                //返回结果表示修改小类别是否成功
                blValue = updateStatus.UpdateLowerCategoryStatus(strLowerCategoryID, blCategoryStatus);

                if (blValue == true)
                {
                    lblMessage.Text = "修改成功";
                    this.txtCategoryName.Text = string.Empty;
                    //下拉框标示为启用
                    dropCategoryStatus.SelectedIndex = 0;
                }
                else
                {
                    lblMessage.Text = "修改失败";
                    this.txtCategoryName.Text = string.Empty;
                }

            }
        }
    }
}